﻿namespace MyGame
{
    public class MapManager : BaseManager<MapManager>
    {
        private bool isCreateChessData;

        public int[,]boardMapData = new int[15,15];

        public Chess[,] chessData = new Chess[15, 15];

        //间隔：65

        private void Awake()
        {
           
        }

        private void Start()
        {
            isCreateChessData = false;
        }

        public void Init()
        {
            if (!isCreateChessData)
            {
                var trans = BoardUI.Instance.chessList;
                Chess[] chesses = trans.GetComponentsInChildren<Chess>();

                foreach (var Chess in chesses)
                {
                    chessData[Chess.row, Chess.arr] = Chess;
                }

                isCreateChessData = true;
            }

            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    boardMapData[i, j] = 0;
                }
            }
        }

        public void UpdateChess(int row, int arr)
        {
            chessData[row, arr].ClickWhite();
        }

        public void UpdateMap(int row, int arr)
        {
            boardMapData[row, arr] = 0;
        }

        public void UpdateMap(int row,int arr,int value)
        {
            boardMapData[row, arr] = value;
        }

        public int[,]  GetMapData()
        {
            return boardMapData;
        }

        public bool IsWingsBlack(int row,int arr)
        {
            int numberHorizontal, nunberVertical, numberTopLeft, numberDownLeft;

            numberHorizontal = ComputeHorizontalCount(1,row,arr);
            nunberVertical = ComputeVerticalCount(1, row, arr);
            numberTopLeft = ComputeTopLeftCount(1, row, arr);
            numberDownLeft = ComputeDownLeftCount(1,row,arr);

            if (numberHorizontal >= 5 || nunberVertical >= 5 || numberTopLeft >= 5 || numberDownLeft >= 5) 
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsWingsWhite(int row, int arr)
        {
            int numberHorizontal, nunberVertical, numberTopLeft, numberDownLeft;

            numberHorizontal = ComputeHorizontalCount(2, row, arr);
            nunberVertical = ComputeVerticalCount(2, row, arr);
            numberTopLeft = ComputeTopLeftCount(2, row, arr);
            numberDownLeft = ComputeDownLeftCount(2, row, arr);

            
            if (numberHorizontal >= 5 || nunberVertical >= 5 || numberTopLeft >= 5 || numberDownLeft >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #region 计算各个方向的棋子

        //计算横方向的棋子
        public int ComputeHorizontalCount(int value,int row,int arr)
        {
            int number = 0; 
            for (int i = arr; i < arr + 5; i++)
            {
                if (i > -1 && i<15)
                {
                    if (boardMapData[row, i] == value)
                    {
                        ++number;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            for (int i = arr-1; i > arr - 5; i--)
            {
                if (i > -1 && i < 15)
                {
                    if (boardMapData[row, i] == value)
                    {
                        ++number;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return number;
        }

        //计算列方向的棋子
        public int ComputeVerticalCount(int value, int row, int arr)
        {
            int number = 0;

            for (int i = row; i < row + 5; i++)
            {
                if (i > -1 && i < 15)
                {
                    if (boardMapData[i, arr] == value)
                    {
                        ++number;

                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            for (int i = row-1; i > row - 5; i--)
            {
                if (i > -1 && i < 15)
                {
                    if (boardMapData[i, arr] == value)
                    {
                        ++number;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return number;
        }

        //计算左上到右下方向的棋子
        public int ComputeTopLeftCount(int value, int row, int arr)
        {
            int number = 0;
          
            int j = arr;
            for (int i = row; i > row - 5; i--)
            {
                if (i > -1 && i < 15 && j > -1 && j < 15)  
                {
                    if (boardMapData[i, j] == value)
                    {
                        ++number;
                        --j;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            int k = arr + 1;
            for (int i = row+1; i < row + 5; i++)
            {
                if (i > -1 && i < 15 && k > -1 && k < 15)
                {
                    if (boardMapData[i, k] == value)
                    {
                        ++number;
                        ++k;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return number;
        }

        //计算左下到右上方向的棋子
        public int ComputeDownLeftCount(int value, int row, int arr)
        {
            int number = 0;

            int j = arr; 
            for (int i = row; i > row - 5; i--)
            {
                if (i > -1 && i < 15 && j > -1 && j < 15)
                {
                    if (boardMapData[i, j] == value)
                    {
                        ++number;
                        ++j;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            int k = arr - 1;
            for (int i = row + 1; i < row + 5; i++)
            {
                if (i > -1 && i < 15 && k > -1 && k < 15)
                {
                    if (boardMapData[i, k] == value)
                    {
                        ++number;
                        --k;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return number;
        }

        #endregion
    }
}

